package mi.payment.ui.admin;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import mi.payment.R;
import mi.payment.constants.Configs;
import mi.payment.settings.NetworkParamSettings;
import mi.payment.ui.base.ParentActivity;
import sunmi.sunmiui.button.ButtonSwitch;
import sunmi.sunmiui.dialog.DialogCreater;
import sunmi.sunmiui.dialog.EditTextDialog;
import sunmi.sunmiui.dialog.ListDialog;
import sunmi.sunmiui.title.Title;

/**
 * 网络参数设置
 * Created by lj on 2017/1/7.
 */
public class CommunicationParameterSettingActivity extends ParentActivity implements View.OnClickListener {
    private View tpduView, hostIPAddressView, hostPortView, automaticallyObtainIPView, backupIPView, backupPortView,
            thirdIpView, thirdPortView, thirdIpBackupView, thirdPortBackupView, communicationTimeoutView, dealsHeavyDialNumberView, placementView,
            defaultNetworkConfigurationView, internetAccessFunctionsView, dnsEnableView;
    private TextView tpduText, hostIPAddressText, hostPortText, communicationTimeoutText, backupIPText, backupPortText,
            thirdIpText, thirdPortText, thirdIpBackupText, thirdPortBackupText, dealsHeavyDialNumberText, placementText, defaultNetworkConfigurationText;
    private ButtonSwitch automaticallyObtainIPSwitch, internetAccessFunctionsSwitch, dnsEnableSwitch;
    private EditTextDialog mEditTextDialog;
    private ListDialog mListDialog;
    private NetworkParamSettings networkParamSettings;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_communication_parameter_setting);
        initView();
        setClickListener();
        updateUI();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        Title mTitle;
        TextView itemLeftText;
        mTitle = (Title) findViewById(R.id.network_parameter_setting_title);
        tpduView = findViewById(R.id.tpdu);
        hostIPAddressView = findViewById(R.id.host_ip_address);
        hostPortView = findViewById(R.id.host_port);
        automaticallyObtainIPView = findViewById(R.id.automatically_obtain_ip);
        backupIPView = findViewById(R.id.backup_ip);
        backupPortView = findViewById(R.id.backup_port);
        thirdIpView = findViewById(R.id.third_ip);
        thirdPortView = findViewById(R.id.third_port);

        thirdIpBackupView = findViewById(R.id.backup_third_ip);
        thirdPortBackupView = findViewById(R.id.backup_third_port);

        communicationTimeoutView = findViewById(R.id.communication_timeout);
        dealsHeavyDialNumberView = findViewById(R.id.deals_heavy_dial_number);
        placementView = findViewById(R.id.placement);
        defaultNetworkConfigurationView = findViewById(R.id.default_network_configuration);
        internetAccessFunctionsView = findViewById(R.id.internet_access_functions);
        dnsEnableView = findViewById(R.id.dns_enable);

        tpduText = (TextView) tpduView.findViewById(R.id.item_right_text);
        hostIPAddressText = (TextView) hostIPAddressView.findViewById(R.id.item_right_text);
        hostPortText = (TextView) hostPortView.findViewById(R.id.item_right_text);
        communicationTimeoutText = (TextView) communicationTimeoutView.findViewById(R.id.item_right_text);
        backupIPText = (TextView) backupIPView.findViewById(R.id.item_right_text);
        backupPortText = (TextView) backupPortView.findViewById(R.id.item_right_text);

        dealsHeavyDialNumberText = (TextView) dealsHeavyDialNumberView.findViewById(R.id.item_right_text);
        placementText = (TextView) placementView.findViewById(R.id.item_right_text);
        defaultNetworkConfigurationText = (TextView) defaultNetworkConfigurationView.findViewById(R.id.item_right_text);

        thirdIpText = (TextView) thirdIpView.findViewById(R.id.item_right_text);
        thirdPortText = (TextView) thirdPortView.findViewById(R.id.item_right_text);

        thirdIpBackupText = (TextView) thirdIpBackupView.findViewById(R.id.item_right_text);
        thirdPortBackupText = (TextView) thirdPortBackupView.findViewById(R.id.item_right_text);

        if (Configs.getInstance().HIDE_PARAMETER_SERVER()) {//隐藏扫码支付服务器
            thirdIpView.setVisibility(View.GONE);
            thirdIpBackupView.setVisibility(View.GONE);
        }
        if (Configs.getInstance().HIDE_PARAMETER_SERVER_PORT()) {//隐藏扫码支付服务器端口
            thirdPortView.setVisibility(View.GONE);
            thirdIpBackupView.setVisibility(View.GONE);
        }

        automaticallyObtainIPSwitch = (ButtonSwitch) automaticallyObtainIPView.findViewById(R.id.set_switch);
        internetAccessFunctionsSwitch = (ButtonSwitch) internetAccessFunctionsView.findViewById(R.id.set_switch);
        dnsEnableSwitch = (ButtonSwitch) dnsEnableView.findViewById(R.id.set_switch);

        itemLeftText = (TextView) tpduView.findViewById(R.id.item_left_text);
        itemLeftText.setText(getResources().getString(R.string.tpdu1));

        itemLeftText = (TextView) hostIPAddressView.findViewById(R.id.item_left_text);
        itemLeftText.setText(getResources().getString(R.string.host_ip_address1));

        itemLeftText = (TextView) hostPortView.findViewById(R.id.item_left_text);
        itemLeftText.setText(getResources().getString(R.string.host_port1));

        itemLeftText = (TextView) automaticallyObtainIPView.findViewById(R.id.switch_item_text);
        itemLeftText.setText(getResources().getString(R.string.automatically_obtain_ip));

        itemLeftText = (TextView) backupIPView.findViewById(R.id.item_left_text);
        itemLeftText.setText(getResources().getString(R.string.backup_ip_address1));

        itemLeftText = (TextView) backupPortView.findViewById(R.id.item_left_text);
        itemLeftText.setText(getResources().getString(R.string.backup_port1));

        itemLeftText = (TextView) communicationTimeoutView.findViewById(R.id.item_left_text);
        itemLeftText.setText(getResources().getString(R.string.communication_timeout1));

        itemLeftText = (TextView) dealsHeavyDialNumberView.findViewById(R.id.item_left_text);
        itemLeftText.setText(getResources().getString(R.string.deals_heavy_dial_number1));

        itemLeftText = (TextView) placementView.findViewById(R.id.item_left_text);
        itemLeftText.setText(getResources().getString(R.string.placement1));

        itemLeftText = (TextView) defaultNetworkConfigurationView.findViewById(R.id.item_left_text);
        itemLeftText.setText(getResources().getString(R.string.default_network_configuration1));

        itemLeftText = (TextView) internetAccessFunctionsView.findViewById(R.id.switch_item_text);
        itemLeftText.setText(getResources().getString(R.string.internet_access_functions));

        itemLeftText = (TextView) dnsEnableView.findViewById(R.id.switch_item_text);
        itemLeftText.setText(getResources().getString(R.string.dns_enable));

        itemLeftText = (TextView) thirdIpView.findViewById(R.id.item_left_text);
        itemLeftText.setText(getResources().getString(R.string.third_server_ip1));

        itemLeftText = (TextView) thirdPortView.findViewById(R.id.item_left_text);
        itemLeftText.setText(getResources().getString(R.string.third_server_port1));

        itemLeftText = (TextView) thirdIpBackupView.findViewById(R.id.item_left_text);
        itemLeftText.setText(getResources().getString(R.string.third_server_ip3));

        itemLeftText = (TextView) thirdPortBackupView.findViewById(R.id.item_left_text);
        itemLeftText.setText(getResources().getString(R.string.third_server_port3));


        mTitle.setButtonVisibility(false, true, true, true);
        mTitle.setHeadBgColor(getResources().getColor(R.color.colorBar));
        mTitle.setTitleText(getResources().getString(R.string.communication_parameter_setting));
        mTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommunicationParameterSettingActivity.this.finish();
            }
        }, null, null, null);

        automaticallyObtainIPSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked && networkParamSettings.isAutomaticGetIp()) {
                    startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
                }
                mNetworkDataOperations.saveAutomaticGetIp(isChecked);
                updateUI();
            }
        });
        internetAccessFunctionsSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mNetworkDataOperations.saveNetworkInterventionFunction(isChecked);
                updateUI();
            }
        });
        dnsEnableSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mNetworkDataOperations.saveDns(isChecked);
                updateUI();
            }
        });
    }

    private void setClickListener() {
        if (Configs.getInstance().UPDATE_NET_PARAMS()) {//是否允许修改网络参数
            tpduView.setOnClickListener(this);
            hostIPAddressView.setOnClickListener(this);
            hostPortView.setOnClickListener(this);
            backupIPView.setOnClickListener(this);
            backupPortView.setOnClickListener(this);
        } else {
            tpduView.setOnClickListener(null);
            hostIPAddressView.setOnClickListener(null);
            hostPortView.setOnClickListener(null);
            backupIPView.setOnClickListener(null);
            backupPortView.setOnClickListener(null);
            tpduView.setBackgroundColor(getResources().getColor(R.color.white));
            backupPortView.setBackgroundColor(getResources().getColor(R.color.white));
            backupIPView.setBackgroundColor(getResources().getColor(R.color.white));
            hostPortView.setBackgroundColor(getResources().getColor(R.color.white));
            hostIPAddressView.setBackgroundColor(getResources().getColor(R.color.white));
            tpduView.findViewById(R.id.enter_img).setVisibility(View.GONE);
            hostIPAddressView.findViewById(R.id.enter_img).setVisibility(View.GONE);
            hostPortView.findViewById(R.id.enter_img).setVisibility(View.GONE);
            backupIPView.findViewById(R.id.enter_img).setVisibility(View.GONE);
            backupPortView.findViewById(R.id.enter_img).setVisibility(View.GONE);
        }
        communicationTimeoutView.setOnClickListener(this);
        dealsHeavyDialNumberView.setOnClickListener(this);
        placementView.setOnClickListener(this);
        defaultNetworkConfigurationView.setOnClickListener(this);
        automaticallyObtainIPView.setOnClickListener(this);
        internetAccessFunctionsView.setOnClickListener(this);
        dnsEnableView.setOnClickListener(this);
        thirdPortView.setOnClickListener(this);
        thirdIpView.setOnClickListener(this);
        thirdPortBackupView.setOnClickListener(this);
        thirdIpBackupView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tpdu:
                //TPDU
                showEditTextDialog(R.string.cancel, R.string.ok, R.string.tpdu1, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mEditTextDialog.dismiss();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (checkEmpty(mEditTextDialog.getText()) && (mEditTextDialog.getText().length() == 10)) {
                            mNetworkDataOperations.saveTPDU(mEditTextDialog.getText());
                            updateUI();
                            mEditTextDialog.dismiss();
                        } else {
                            showToast(getString(R.string.error_input));
                        }
                    }
                }, networkParamSettings.getTPdu(), new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialog) {
                        mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                        mEditTextDialog.setMaxLength(10);
                        showSoftKeyboard(mEditTextDialog);
                    }
                });
                break;
            case R.id.host_ip_address:
                //主机IP地址
                showEditTextDialog(R.string.cancel, R.string.ok, R.string.host_ip_address1, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mEditTextDialog.dismiss();
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (checkEmpty(mEditTextDialog.getText())) {
                                    if (isboolIp(mEditTextDialog.getText())) {
                                        mNetworkDataOperations.saveHostIpAddress(mEditTextDialog.getText());
                                        updateUI();
                                        mEditTextDialog.dismiss();
                                    } else {
                                        showToast(getString(R.string.error_input));
                                    }
                                }
                            }
                        }, networkParamSettings.getHostIpAddress(), new DialogInterface.OnShowListener()

                        {
                            @Override
                            public void onShow(DialogInterface dialog) {
                                mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_PHONE);
                                showSoftKeyboard(mEditTextDialog);
                            }
                        }
                );
                break;
            case R.id.host_port:
                //主机端口
                showEditTextDialog(R.string.cancel, R.string.ok, R.string.host_port1, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mEditTextDialog.dismiss();
                            }
                        }, new View.OnClickListener()

                        {
                            @Override
                            public void onClick(View v) {
                                if (checkEmpty(mEditTextDialog.getText())) {
                                    if (checkPort(mEditTextDialog.getText())) {
                                        mNetworkDataOperations.saveHostPort(mEditTextDialog.getText());
                                        updateUI();
                                    } else {
                                        showToast(getString(R.string.error_input));
                                    }
                                    mEditTextDialog.dismiss();
                                }
                            }
                        }, networkParamSettings.getHostPort(), new DialogInterface.OnShowListener()

                        {
                            @Override
                            public void onShow(DialogInterface dialog) {
                                mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                                showSoftKeyboard(mEditTextDialog);
                            }
                        }
                );
                break;
            case R.id.communication_timeout:
                //通讯超时时间
                showEditTextDialog(R.string.cancel, R.string.ok, R.string.communication_timeout1, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mEditTextDialog.dismiss();
                            }
                        }, new View.OnClickListener()

                        {
                            @Override
                            public void onClick(View v) {
                                if (checkEmpty(mEditTextDialog.getText())) {
                                    if (Integer.parseInt(mEditTextDialog.getText()) >= 1 && Integer.parseInt(mEditTextDialog.getText()) <= 90) {
                                        mNetworkDataOperations.saveCommunicationTimeout(mEditTextDialog.getText());
                                        updateUI();
                                        mEditTextDialog.dismiss();
                                    } else {
                                        showToast(getString(R.string.error_input_time));
                                    }

                                }
                            }
                        }, networkParamSettings.getCommunicationTimeout(), new DialogInterface.OnShowListener()

                        {
                            @Override
                            public void onShow(DialogInterface dialog) {
                                mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                                mEditTextDialog.setMaxLength(2);
                                showSoftKeyboard(mEditTextDialog);
                            }
                        }
                );
                break;
            case R.id.backup_ip:
                //备份服务器IP
                showEditTextDialog(R.string.cancel, R.string.ok, R.string.backup_ip_address1, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mEditTextDialog.dismiss();
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (TextUtils.isEmpty(mEditTextDialog.getText()) || isboolIp(mEditTextDialog.getText())) {
                                    mNetworkDataOperations.saveSpareHostIpAddress(mEditTextDialog.getText());
                                    updateUI();
                                    mEditTextDialog.dismiss();
                                } else {
                                    showToast(getString(R.string.error_input));
                                }
                            }
                        }, networkParamSettings.getSpareHostIpAddress(), new DialogInterface.OnShowListener()

                        {
                            @Override
                            public void onShow(DialogInterface dialog) {
                                mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_PHONE);
                                showSoftKeyboard(mEditTextDialog);
                            }
                        }
                );
                break;

            case R.id.third_ip:
                //备份服务器IP
                showEditTextDialog(R.string.cancel, R.string.ok, R.string.third_server_ip1, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mEditTextDialog.dismiss();
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (TextUtils.isEmpty(mEditTextDialog.getText()) || isboolIp(mEditTextDialog.getText())) {
                                    mNetworkDataOperations.saveThirdServerIpAddress(mEditTextDialog.getText());
                                    updateUI();
                                    mEditTextDialog.dismiss();
                                } else {
                                    showToast(getString(R.string.error_input));
                                }
                            }
                        }, networkParamSettings.getThirdServerIpAddress(), new DialogInterface.OnShowListener()

                        {
                            @Override
                            public void onShow(DialogInterface dialog) {
                                mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_PHONE);
                                showSoftKeyboard(mEditTextDialog);
                            }
                        }
                );
                break;

            case R.id.backup_port:
                //备份服务器端口
                showEditTextDialog(R.string.cancel, R.string.ok, R.string.backup_port1, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mEditTextDialog.dismiss();
                    }
                }, new View.OnClickListener()

                {
                    @Override
                    public void onClick(View v) {


                        if (checkEmpty(mEditTextDialog.getText())) {
                            if (checkPort(mEditTextDialog.getText())) {
                                mNetworkDataOperations.saveSpareHostPort(mEditTextDialog.getText());
                                updateUI();
                            } else {
                                showToast(getString(R.string.error_input));
                            }

                            mEditTextDialog.dismiss();
                        }

                    }
                }, networkParamSettings.getSpareHostPort(), new DialogInterface.OnShowListener()

                {
                    @Override
                    public void onShow(DialogInterface dialog) {
                        mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                        showSoftKeyboard(mEditTextDialog);
                    }
                });
                break;

            case R.id.third_port:
                //备份服务器端口
                showEditTextDialog(R.string.cancel, R.string.ok, R.string.third_server_port1, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mEditTextDialog.dismiss();
                    }
                }, new View.OnClickListener()

                {
                    @Override
                    public void onClick(View v) {

                        if (checkEmpty(mEditTextDialog.getText())) {
                            if (checkPort(mEditTextDialog.getText())) {
                                mNetworkDataOperations.saveThirdServerPort(mEditTextDialog.getText());
                                updateUI();
                            } else {
                                showToast(getString(R.string.error_input));
                            }
                            mEditTextDialog.dismiss();
                        }
                    }
                }, networkParamSettings.getThirdServerPort(), new DialogInterface.OnShowListener()

                {
                    @Override
                    public void onShow(DialogInterface dialog) {
                        mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                        showSoftKeyboard(mEditTextDialog);
                    }
                });
                break;

            case R.id.backup_third_ip:
                //备份服务器IP
                showEditTextDialog(R.string.cancel, R.string.ok, R.string.third_server_ip3, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mEditTextDialog.dismiss();
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (TextUtils.isEmpty(mEditTextDialog.getText()) || isboolIp(mEditTextDialog.getText())) {
                                    mNetworkDataOperations.saveThirdServerIpAddressBackup(mEditTextDialog.getText());
                                    updateUI();
                                    mEditTextDialog.dismiss();
                                } else {
                                    showToast(getString(R.string.error_input));
                                }
                            }
                        }, networkParamSettings.getThirdServerIpAddressBackup(), new DialogInterface.OnShowListener()

                        {
                            @Override
                            public void onShow(DialogInterface dialog) {
                                mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_PHONE);
                                showSoftKeyboard(mEditTextDialog);
                            }
                        }
                );
                break;

            case R.id.backup_third_port:
                //备份服务器端口
                showEditTextDialog(R.string.cancel, R.string.ok, R.string.third_server_port1, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mEditTextDialog.dismiss();
                    }
                }, new View.OnClickListener()

                {
                    @Override
                    public void onClick(View v) {

                        if (checkEmpty(mEditTextDialog.getText())) {
                            if (checkPort(mEditTextDialog.getText())) {
                                mNetworkDataOperations.saveThirdServerPortBackup(mEditTextDialog.getText());
                                updateUI();
                            } else {
                                showToast(getString(R.string.error_input));
                            }
                            mEditTextDialog.dismiss();
                        }
                    }
                }, networkParamSettings.getThirdServerPortBackup(), new DialogInterface.OnShowListener()

                {
                    @Override
                    public void onShow(DialogInterface dialog) {
                        mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                        showSoftKeyboard(mEditTextDialog);
                    }
                });
                break;
            case R.id.deals_heavy_dial_number:

                //交易重拨次数
                showEditTextDialog(R.string.cancel, R.string.ok, R.string.deals_heavy_dial_number1, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mEditTextDialog.dismiss();
                            }
                        }, new View.OnClickListener()

                        {
                            @Override
                            public void onClick(View v) {
                                if (checkEmpty(mEditTextDialog.getText()) && checkInt(mEditTextDialog.getText(), 3, 1)) {
                                    mNetworkDataOperations.saveTransactionRepeatNum(Integer.parseInt(mEditTextDialog.getText()));
                                    updateUI();
                                    mEditTextDialog.dismiss();
                                }
                            }
                        }, networkParamSettings.getTransactionRepeatNum() + "", new DialogInterface.OnShowListener()

                        {
                            @Override
                            public void onShow(DialogInterface dialog) {
                                mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                                mEditTextDialog.setMaxLength(1);
                                showSoftKeyboard(mEditTextDialog);
                            }
                        }

                );
                break;
            case R.id.default_network_configuration:
                //默认网络配置
                String[] defaultNetwork = {getResources().getString(R.string.default_network1), getResources().getString(R.string.default_network2)};
                mListDialog = DialogCreater.createListDialog(CommunicationParameterSettingActivity.this, getResources().getString(R.string.set_to), getResources().getString(R.string.cancel), defaultNetwork);
                mListDialog.setItemClickListener(new ListDialog.ItemClickListener() {
                    @Override
                    public void OnItemClick(int position) {
                        switch (position) {
                            case 0:
                                mNetworkDataOperations.saveNetworkDefaultConfiguration(1);
                                break;
                            case 1:
                                mNetworkDataOperations.saveNetworkDefaultConfiguration(2);
                                break;
                            default:
                                break;
                        }
                        updateUI();
                        mListDialog.dismiss();
                    }
                });
                mListDialog.show();
                break;
            case R.id.placement:
                //连接方式
                String[] placement = {getResources().getString(R.string.short_placement), getResources().getString(R.string.long_placement)};
                mListDialog = DialogCreater.createListDialog(CommunicationParameterSettingActivity.this, getResources().getString(R.string.set_to), getResources().getString(R.string.cancel), placement);
                mListDialog.setItemClickListener(new ListDialog.ItemClickListener() {
                    @Override
                    public void OnItemClick(int position) {
                        mNetworkDataOperations.saveConnectionMode(position);
                        updateUI();
                        mListDialog.dismiss();
                    }
                });
                mListDialog.show();
                break;
            case R.id.dns_enable:
                dnsEnableSwitch.setOnCheck(!dnsEnableSwitch.isChecked());
                break;
            case R.id.internet_access_functions:
                internetAccessFunctionsSwitch.setOnCheck(!internetAccessFunctionsSwitch.isChecked());
                break;
            case R.id.automatically_obtain_ip:
                automaticallyObtainIPSwitch.setOnCheck(!automaticallyObtainIPSwitch.isChecked());
                break;
        }
    }


    /**
     * 检测 port 合法性
     *
     * @param port
     * @return
     */
    private boolean checkPort(String port) {
        int p = 0;
        try {
            p = Integer.parseInt(port);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (p > 0) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 显示输入弹框
     *
     * @param leftTextId  弹框左按钮文字ID
     * @param rightTextId 弹框右按钮文字ID
     * @param titleTextId 弹框标题ID
     * @param leftClick   弹框左按钮点击事件
     * @param rightClick  弹框右按钮点击时间
     */

    private void showEditTextDialog(int leftTextId, int rightTextId, int titleTextId, View.OnClickListener leftClick, View.OnClickListener rightClick, String text, DialogInterface.OnShowListener onShow) {
        mEditTextDialog = DialogCreater.createEditTextDialog(this, getResources().getString(leftTextId), getResources().getString(rightTextId), getResources().getString(titleTextId), leftClick, rightClick, onShow);
        if (TextUtils.isEmpty(text)) {
            mEditTextDialog.setHintText(getResources().getString(R.string.return_unknow));
        } else {
            mEditTextDialog.setText(text);
        }

        mEditTextDialog.show();
    }


    /**
     * 更新View
     */
    private void updateUI() {
        networkParamSettings = mNetworkDataOperations.getNetworkParamSettings();
        if (TextUtils.isEmpty(networkParamSettings.getTPdu())) {
            tpduText.setText(getResources().getString(R.string.tpdu2));
        } else {
            tpduText.setText(networkParamSettings.getTPdu());
        }
        if (TextUtils.isEmpty(networkParamSettings.getHostIpAddress())) {
            hostIPAddressText.setText(getResources().getString(R.string.host_ip_address2));
        } else {
            hostIPAddressText.setText(networkParamSettings.getHostIpAddress());
        }

        if (TextUtils.isEmpty(networkParamSettings.getHostPort())) {
            hostPortText.setText(getResources().getString(R.string.host_port2));
        } else {
            hostPortText.setText(networkParamSettings.getHostPort());
        }

        if (TextUtils.isEmpty(networkParamSettings.getSpareHostIpAddress())) {
            backupIPText.setText(getResources().getString(R.string.backup_ip_address2));
        } else {
            backupIPText.setText(networkParamSettings.getSpareHostIpAddress());
        }

        if (TextUtils.isEmpty(networkParamSettings.getSpareHostPort())) {
            backupPortText.setText(getResources().getString(R.string.backup_port2));
        } else {
            backupPortText.setText(networkParamSettings.getSpareHostPort());
        }

        if (TextUtils.isEmpty(networkParamSettings.getThirdServerIpAddress())) {
            thirdIpText.setText(getResources().getString(R.string.third_server_ip2));
        } else {
            thirdIpText.setText(networkParamSettings.getThirdServerIpAddress());
        }

        if (TextUtils.isEmpty(networkParamSettings.getThirdServerPort())) {
            thirdPortText.setText(getResources().getString(R.string.third_server_port2));
        } else {
            thirdPortText.setText(networkParamSettings.getThirdServerPort());
        }

        if (TextUtils.isEmpty(networkParamSettings.getThirdServerIpAddressBackup())) {
            thirdIpBackupText.setText("");
        } else {
            thirdIpBackupText.setText(networkParamSettings.getThirdServerIpAddressBackup());
        }

        if (TextUtils.isEmpty(networkParamSettings.getThirdServerPortBackup())) {
            thirdPortBackupText.setText("");
        } else {
            thirdPortBackupText.setText(networkParamSettings.getThirdServerPortBackup());
        }

        if (TextUtils.isEmpty(networkParamSettings.getCommunicationTimeout())) {
            communicationTimeoutText.setText(getResources().getString(R.string.communication_timeout2));
        } else {
            communicationTimeoutText.setText(networkParamSettings.getCommunicationTimeout());
        }

        if (networkParamSettings.getTransactionRepeatNum() == -1) {
            dealsHeavyDialNumberText.setText(getResources().getString(R.string.deals_heavy_dial_number2));
        } else {
            dealsHeavyDialNumberText.setText(String.valueOf(networkParamSettings.getTransactionRepeatNum()));
        }

        if (networkParamSettings.getConnectionMode() == 0) {
            placementText.setText(getString(R.string.short_placement));
        } else if (networkParamSettings.getConnectionMode() == 1) {
            placementText.setText(getString(R.string.long_placement));
        } else {
            placementText.setText(getResources().getString(R.string.placement2));
        }

        if (networkParamSettings.getNetworkDefaultConfiguration() == 1) {
            defaultNetworkConfigurationText.setText(getResources().getString(R.string.default_network1));
        } else if (networkParamSettings.getNetworkDefaultConfiguration() == 2) {
            defaultNetworkConfigurationText.setText(getResources().getString(R.string.default_network2));
        } else {
            defaultNetworkConfigurationText.setText(getResources().getString(R.string.default_network_configuration2));
        }
        automaticallyObtainIPSwitch.setOnCheck(networkParamSettings.isAutomaticGetIp());
        internetAccessFunctionsSwitch.setOnCheck(networkParamSettings.isNetworkInterventionFunction());
        dnsEnableSwitch.setOnCheck(networkParamSettings.isDns());
    }

    private boolean isboolIp(String ipAddress) {
        String ip = "((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))";
        Pattern pattern = Pattern.compile(ip);
        Matcher matcher = pattern.matcher(ipAddress);
        return matcher.matches();
    }
}
